package com.zspringboot.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zspringboot.demo.common.Result;
import com.zspringboot.demo.entity.Dict;
import com.zspringboot.demo.entity.Menu;
import com.zspringboot.demo.service.IDictService;
import com.zspringboot.demo.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author laolang
 * @since 2024-02-25
 */
@RestController
@RequestMapping("/menu")
public class MenuController {


    @Resource
    private IMenuService menuService;

    @Autowired
    private IDictService dictService;
    // 新增/修改
    @PostMapping
    public Result save(@RequestBody Menu menu) {
            return Result.success(menuService.saveOrUpdate(menu));
            }

    // 删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
            return Result.success(menuService.removeById(id));
            }
    // 批量删除
    @PostMapping("/del/batch")
    public Result deleteBacth(@RequestBody List<Integer> ids){
        return Result.success(menuService.removeByIds(ids));
        }
    // 查询所有数据
    @GetMapping
    public Result findAll(@RequestParam(defaultValue = "") String name) {
//        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
//        queryWrapper.orderByDesc("id");
//        if (!"".equals(name))
//            queryWrapper.like("name",name);
//        List<Menu> list = menuService.list(queryWrapper);
//        // 找出pid为null 的一级菜单
//        List<Menu> parentNode = list.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
//        // 找出一级菜单的子菜单
//        for (Menu menu: parentNode){
//            menu.setChildren(
//                    list.stream()
//                            .filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
//        }
        List<Menu> menus = menuService.findMenus(name);
        return Result.success(menus);
    }


    @GetMapping("/ids")
    public Result findAllIds(){
        // 查出 所有的 Menu id
        return Result.success(menuService.list().stream().map(Menu::getId));
    }

    // 根据id 查询
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
            return Result.success(menuService.getById(id));
            }

    // 分页
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize,
                            @RequestParam(defaultValue = "") String name) {
            QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
//            queryWrapper.orderByDesc("id");
            if (!"".equals(name))
            queryWrapper.like("name",name);
            return Result.success(menuService.page(new Page<>(pageNum, pageSize),queryWrapper));
            }


    // 查询icon
    @GetMapping("/icons")
    public Result getIcons() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("type","icon");
        return Result.success(dictService.list(queryWrapper));
    }


}

